﻿using System;
using System.Globalization;
using System.Linq;
using System.Windows.Data;
using Labthink.PCSoft.UniversalHost4.Utility;

namespace Labthink.PCSoft.UniversalHost4.Instrument.Converters
{
    public class PermissionConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (Common.BaseInfo == null)
            {
                return true;
            }

            if (!Common.BaseInfo.Gmp) return true;
            //if (value == null) return true;
            if (Common.ParamForTraces == null) return true;
            var pt = Common.ParamForTraces.SingleOrDefault(p => p.DataNo == parameter.ToInt());
            if (pt == null) return true;
            if (Common.Permissions.All(p => p.Code != parameter + "")) return false;
            return true;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }
}
